﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
namespace DAL
{
    /// <summary>
    /// 商品的  SKU处理相关操作
    /// </summary>
    public class sp_ProductServcie
    {
        MyShoppingDB_VueEntities nn = new MyShoppingDB_VueEntities();
        #region 查询指定商品id下生成的所有sku货品 ,本操作，需要处理一些业务
        /*
            规则说明：如果商品的SKU 货品已经生成则不再生成,如果暂末生成，则先通过生成再查询
         */
        /// <summary>
        //
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        public List<sp_Product> GetAllProductByGoods(int goodId)
        {
            /////1.先查询是不是已存在////////////
            string selsql = "select * from [sp_Product] where [Goods_id]="+goodId;
            List<sp_Product> numlist = DapperTools.Query<sp_Product>(selsql);
            /////2.判断//////////
            if (numlist.Count == 0) 
            {
                // 需要批量生成该商品下的货品
                //2.1 先用goodid查询该商品的动态参数,及值
                string goods_attr_sql = @"select spattr.attr_id,
                        spattr.attr_name,gattr.attr_value from sp_goods as g
                        inner join sp_goods_attr as gattr 
                        on g.goods_id= gattr.goods_id --商品参数表关联
                        inner join [sp_attribute] as spattr 
                        on gattr.attr_id=spattr.attr_id
                        where g.goods_id="+goodId+" and spattr.attr_sel='many' ";

                 List<GoodAttr> goodAttrs = DapperTools.Query<GoodAttr>(goods_attr_sql);
                //把动态参数转换为集合的集合，形式如List< List<> >为了调用递归生成
                List< List<string> > productArrtList = new List< List<string> >();
                foreach (GoodAttr goodAttr in goodAttrs)
                {
                    //黑色,白色,绿色
                    string attrs = goodAttr.attr_value;//读取每一次的值
                    //转成集合
                    List<string> attslist = attrs.Split(',').ToList();
                    //加入到集合的集合中
                    productArrtList.Add(attslist); //完成多种参数的转换
                }
                //3.通过递归调用进行参数之间的组合
                List<string> reAttrList = new List<string >(); //组合出来的参数字符串

                
                if (productArrtList.Count > 0)
                {
                   JSAttr.DHG2("", 0, productArrtList[0], productArrtList, ref reAttrList);
                }
                //4.查询出一条商品的信息
                string oneGoodSQL = "select * from sp_goods where goods_id="+goodId;
                List<sp_goods> oneGoods=DapperTools.Query<sp_goods>(oneGoodSQL);
                //5.通过循环把商品的信息，通过SKU组合成货品信息，如没有参数，
                //则直接把商品相关信息放到产品表
                List<sp_Product> sp_ProductsList = new List<sp_Product>();
                if (reAttrList.Count > 0)
                {
                  
                    foreach (string str in reAttrList)
                    {
                        sp_Product proobj = new sp_Product();
                        proobj.Goods_id = oneGoods[0].goods_id;
                        proobj.Good_name= oneGoods[0].goods_name;
                        proobj.Good_SKUName = oneGoods[0].goods_name;
                        proobj.Good_Price=oneGoods[0].goods_price;
                        proobj.Good_SKU_KC = 0;//库存
                        proobj.Good_SKU_KCPrice = 0;
                        proobj.Good_SKU_VIPPrice= oneGoods[0].goods_price;// vip销售价
                        proobj.Good_SKU_Attr = str;//组合成的属性
                        proobj.Good_SKU_AttrCount = productArrtList.Count;//参数数量
                        proobj.Good_SKU_ShowType = 0;//显示在一起
                        proobj.Good_Content = oneGoods[0].goods_introduce;//描述
                        proobj.Good_showpic1 = oneGoods[0].goods_big_logo;
                        proobj.Good_showpic2 = oneGoods[0].goods_small_logo;
                        proobj.bei1 = "";
                        proobj.bei2 = "";

                        // 放到到集合中
                        sp_ProductsList.Add(proobj);
                    }

                }
                else 
                {
                    //如果没有参数的情况，只有参数无
                    sp_Product proobj = new sp_Product();
                    proobj.Goods_id = oneGoods[0].goods_id;
                    proobj.Good_name = oneGoods[0].goods_name;
                    proobj.Good_SKUName = oneGoods[0].goods_name;
                    proobj.Good_Price = oneGoods[0].goods_price;
                    proobj.Good_SKU_KC = 0;//库存
                    proobj.Good_SKU_KCPrice = 0;
                    proobj.Good_SKU_VIPPrice = oneGoods[0].goods_price;// vip销售价
                    proobj.Good_SKU_Attr = "无";//组合成的属性
                    proobj.Good_SKU_AttrCount = productArrtList.Count;//参数数量
                    proobj.Good_SKU_ShowType = 0;//显示在一起
                    proobj.Good_Content = oneGoods[0].goods_introduce;//描述
                    proobj.Good_showpic1 = oneGoods[0].goods_big_logo;
                    proobj.Good_showpic2 = oneGoods[0].goods_small_logo;
                    proobj.bei1 = "";
                    proobj.bei2 = "";

                    // 放到到集合中
                    sp_ProductsList.Add(proobj);
                }
                //6. 批量增加
                string add_product_SQL = @"insert into [sp_Product] values(@Goods_id, @Good_name, @Good_SKUName, 
                        @Good_Price, @Good_SKU_KC, @Good_SKU_KCPrice, @Good_SKU_VIPPrice,
                         @Good_SKU_Attr, @Good_SKU_ShowType, @Good_SKU_AttrCount, @Good_Content, 
                        @Good_showpic1, @Good_showpic2, @bei1, @bei2)";
                //调用Dapper
               int n=  DapperTools.Add(add_product_SQL, sp_ProductsList);


            }
            /////////////////////////////
            string sql = "select * from sp_Product where Goods_id="+goodId;
            List<sp_Product> spProductList = DapperTools.Query<sp_Product>(sql);
            return spProductList;
        }
        #endregion

        #region 查询所有
        public List<sp_Product> cx()
        {
            return nn.Set<sp_Product>().ToList();
        }
        #endregion
    }

    //为了查询更好的返回数据，可能定义查询结果一样的实体类，用业接受查询的结果
    /*
        
     */
    public class GoodAttr
    { 
       public int attr_id { get; set; }
       public string attr_name { get; set; }
       
       public string attr_value { get; set; }
    }

    /// <summary>
    /// 计算商品参数组合的递归方法，放在这类中
    /// </summary>
    public class JSAttr
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="nn">nn为连接好的字符串</param>
        /// <param name="c"> c长度</param>
        /// <param name="L">参数长</param>
        /// <param name="ooop">集合的集合</param>
        /// <param name="reAttrList">用来保存的集合</param>
       public  static void DHG2(string nn, int c, List<string> L,
           List<List<string>> ooop, ref List<string> reAttrList)
        {
            c = c + 1;
            foreach (var value in L)
            {
                if (ooop.Count > c)
                {
                    var mm = nn + value + ",";

                    DHG2(mm, c, ooop[c], ooop, ref reAttrList); 
                    //这里用了递归？什么是递归？方法中调用方法本身，必须有一个退出功能
                }
                else
                {
                    reAttrList.Add(nn + value);
                }
            }
        }
    }
}
